package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

//菜品
@RestController
public class DishController {
	
	@Autowired
	private DishService dishService;
	@Autowired
	private RedisTemplate redisTemplate;
	
	
	@GetMapping("/dish/page")
	public ResultInfo findByPage(
			@RequestParam(value = "page", defaultValue = "1") Integer pageNum,//第几页,默认为1
			@RequestParam(defaultValue = "10") Integer pageSize,//每页大小,默认为10
			String name
	) {
		//调用service分页查询
		Page<Dish> byPage = dishService.findByPage(name, pageNum, pageSize);
		return ResultInfo.success(byPage);
	}
	//新增菜品
	@PostMapping("/dish")
	public ResultInfo save(@RequestBody Dish dish) {
		//新增菜品时删除Redis缓存
		//获取相关的key集合
		Set keys = redisTemplate.keys("dish_*");
		// 根据key集合删除
		redisTemplate.delete(keys);
		
		dishService.save(dish);
		return ResultInfo.success(null);
	}
	
	@GetMapping("/dish/{id}")
	public ResultInfo getDish(@PathVariable Long id) {
		
		return ResultInfo.success(dishService.findById(id));
	}
	//修改菜品
	@PutMapping("/dish")
	public ResultInfo update(@RequestBody Dish dish) {
		//新增菜品时删除Redis缓存
		//获取相关的key集合
		Set keys = redisTemplate.keys("dish_*");
		// 根据key集合删除
		redisTemplate.delete(keys);
		dishService.update(dish);
		return ResultInfo.success(null);
	}
	@PostMapping("/dish/status/{statu}")
	public ResultInfo batchStartAndStop(@PathVariable("statu") Integer statu,
	                                    @RequestParam("ids")List<String> ids) {
		
		//新增菜品时删除Redis缓存
		//获取相关的key集合
		Set keys = redisTemplate.keys("dish_*");
		// 根据key集合删除
		redisTemplate.delete(keys);
		
		dishService.startAndStop(statu,ids);
		return ResultInfo.success(null);
	}
	
	@DeleteMapping("/dish")
	public ResultInfo batchDeleteDish(@RequestParam("ids") List<String> ids){
		//新增菜品时删除Redis缓存
		//获取相关的key集合
		Set keys = redisTemplate.keys("dish_*");
		// 根据key集合删除
		redisTemplate.delete(keys);
		dishService.delete(ids);
		
		return ResultInfo.success(null);
	}
	@RequestMapping("/dish/list")
	public ResultInfo findByCategoryId(Long categoryId, String name) {
		if(categoryId != null){
			List<Dish> dishList = dishService.findByCategoryId(categoryId);
			return ResultInfo.success(dishList);
		}
		if (name != null) {
			List<Dish> disheList = dishService.searchDishByName(name);
			return ResultInfo.success(disheList);
		}

		return ResultInfo.error("参数错误");
	}
	
	// @RequestMapping("/dish/list")
	// public ResultInfo findByCategoryId(Long categoryId) {
	// 	List<Dish> dishList = dishService.findByCategoryId(categoryId);
	// 		return ResultInfo.success(dishList);
	// }
	// @GetMapping("/dish/list")
	// public ResultInfo searchDishByName(String name) {
	// 	List<Dish> disheList = dishService.searchDishByName(name);
	// 	return ResultInfo.success(disheList);
	// }
}
